package test.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class upd客户端 {
	 
	private static String host = "192.168.0.144";
 
	public static void main(String[] args) {
 
		String sendMsg = "Hello, 我是乙方, 开始发送数据,端口从 乙方.45671-->甲方.44300 ";
		byte[] buf = sendMsg.getBytes();
		System.out.println("我是乙方,开始发送数据");
		try {
			InetAddress address = InetAddress.getByName(host);
			int port = 44300;
			// 创建发送方的数据报信息
			DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, address, port);
			// 创建socket
			DatagramSocket socketSender = new DatagramSocket(45672);
		//	InetSocketAddress addr = new InetSocketAddress( 45671);
			//socketSender.bind(addr);
			// 发送信息
			socketSender.send(datagramPacket);
 
			// 接收服务器反馈的数据
			byte[] backMsg = new byte[1024];
			DatagramPacket backPacket = new DatagramPacket(backMsg, backMsg.length);
			socketSender.receive(backPacket);
			String recvMsg = new String(backMsg, 0, backPacket.getLength());
			System.out.println("收到甲方反馈数据: " + recvMsg);
			InetAddress clientAddress = backPacket.getAddress();
			 int clientPort = backPacket.getPort();
			System.out.println("甲方的IP和port是： " + clientAddress + "   " + clientPort);  
			socketSender.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
 
		}
	}
 
}